$fields=implode(',',array_map('mysql_real_escape_string',$fields));
$table=mysql_real_escape_string($table);
$limit=is_set($limit)?mysql_real_escape_string($limit):'';
$q=$db->prepare("select $fields from $table $limit");
$q->execute();
while($row=$q->fetch())$proc($row);

/*
unset($fields);
unset($table);
unset($limit);
unset($q);
*/